//
//  PrivacyPolicyAlertView.m
//  ZAYY
//
//  Created by Vic on 2019/4/2.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "PrivacyPolicyAlertView.h"

@implementation PrivacyPolicyAlertView

- (instancetype)initWithTarget:(id)target {
    self = [super init];
    if (self) {
        [self initUI];
        self.delegate = target;
    }
    return self;
}
- (void)initUI {
    self.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    self.backgroundColor = [UIColor colorWithWhite:0.01 alpha:0.5];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    UIView *bgView = [UIToolsControl initViewWithFrame:CGRectMake(0, 0, WIDTH-80*Scale, ScalePS(260)) backgroundColor:COLOR_OF_WHITE];
    bgView.center = self.center;
    [self addSubview:bgView];
    
    UILabel *titleLbl = [UIToolsControl initLabelWithFrame:CGRectMake(0, 10*Scale, ViewWidth(bgView), 50*Scale) text:@"服务协议和隐私政策" textColor:THEMECOLOR_OF_TEXT_333333 font:Font_20];
    titleLbl.font = FontBold(Font_18);
    titleLbl.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:titleLbl];
    
//    UILabel *detailLbl = [UIToolsControl initLabelWithFrame:CGRectMake(15*Scale, EndY(titleLbl), ViewWidth(bgView)-30*Scale, 30*Scale) text:@"本《隐私政策》将向您说明：" textColor:THEMECOLOR_OF_TEXT_333333 font:Font_17];
//    [bgView addSubview:detailLbl];
    
    NSString *contentStr = @"请您务必谨慎阅读，充分理解“服务协议”和“隐私政策”各条款，您可以在“关于我们”中查看，也可以阅读《服务协议》和《隐私政策》了解详细信息。如你同意，请点击“同意”开始接受我们的服务。";
    UILabel *contentLbl = [UIToolsControl initLabelWithFrame:CGRectMake(15*Scale, EndY(titleLbl), ViewWidth(bgView)-30*Scale, 145*Scale) text:contentStr textColor:THEMECOLOR_OF_TEXT_666666 font:Font_15];
    contentLbl.numberOfLines = 0;
    contentLbl.lineBreakMode = NSLineBreakByWordWrapping;
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:LineSpacingHeight];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:contentStr attributes:@{NSFontAttributeName:Font(Font_13), NSForegroundColorAttributeName:THEMECOLOR_OF_TEXT_666666, NSParagraphStyleAttributeName:paragraphStyle}];
    [string addAttribute:NSFontAttributeName value:Font(Font_15) range:NSMakeRange(0, contentStr.length)];
    [string addAttribute:NSForegroundColorAttributeName value:THEMECOLOR_OF_TEXT_666666 range:NSMakeRange(0, contentStr.length)];
    [string addAttributes:@{NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle], NSForegroundColorAttributeName:THEMECOLOR_OF_ORANGE} range:NSMakeRange(49, 6)];
    [string addAttributes:@{NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle], NSForegroundColorAttributeName:THEMECOLOR_OF_ORANGE} range:NSMakeRange(56, 6)];
    contentLbl.attributedText = string;
    [bgView addSubview:contentLbl];
    __weak __typeof(self)weakSelf = self;
    [contentLbl yb_addAttributeTapActionWithStrings:@[@"《服务协议》", @"《隐私政策》"] tapClicked:^(NSString *string, NSRange range, NSInteger index) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        if ([string isEqualToString:@"《服务协议》"]) {
            NSLog(@"服务协议");
            [strongSelf alertTap:YES];
        } else {
            NSLog(@"隐私政策");
            [strongSelf alertTap:NO];
        }
    }];
    
    
    UIButton *browseBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, EndY(contentLbl)+ScalePS(10), ViewWidth(bgView)*0.5, 45*Scale) imageName:nil target:self action:@selector(browseOrOkBtnTap) title:@"仅浏览" titleColor:THEMECOLOR_OF_TEXT_666666];
    browseBtn.titleLabel.font = Font(Font_17);
    [bgView addSubview:browseBtn];
    [browseBtn borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeTop];
    
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(EndX(browseBtn), StartY(browseBtn), ViewWidth(bgView)*0.5, 45*Scale) imageName:nil target:self action:@selector(browseOrOkBtnTap) title:@"同意" titleColor:THEMECOLOR_OF_TEXT_333333];
    okBtn.titleLabel.font = Font(Font_17);
    [bgView addSubview:okBtn];
    [okBtn borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:(BorderSideTypeTop | BorderSideTypeLeft)];
}
- (void)alertTap:(BOOL)flag {
    // flag:Yes服务协议, NO：隐私政策
    if (self.delegate && [self.delegate respondsToSelector:@selector(privacyPolicyAlertView:tapItem:)]) {
        [self.delegate privacyPolicyAlertView:self tapItem:(flag?GeneralWebViewTypeOfResignAgreement:GeneralWebViewTypeOfPrivacyPolicy)];
    }
}
- (void)browseOrOkBtnTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(privacyPolicyAlertViewBrowseOrOkTap:)]) {
        [self.delegate privacyPolicyAlertViewBrowseOrOkTap:self];
    }
}

@end
